package client;

import java.io.IOException;
import java.util.Scanner;

public class UIManager {
    private final RequestHandler requestHandler = new RequestHandler();

    public void displayMenu() throws IOException {
        Scanner scanner = new Scanner(System.in);
        while (true) {
            System.out.println("==== 个人通讯录 ====");
            System.out.println("1. 查看联系人");
            System.out.println("2. 增加联系人");
            System.out.println("3. 删除联系人");
            System.out.println("4. 更新联系人");
            System.out.println("5. 退出");
            System.out.print("请选择操作：");

            int choice = scanner.nextInt();
            scanner.nextLine(); // consume newline

            switch (choice) {
                case 1:
                    requestHandler.viewContacts();
                    break;
                case 2:
                    System.out.print("请输入姓名：");
                    String name = scanner.nextLine();
                    System.out.print("请输入电话号码：");
                    String phone = scanner.nextLine();
                    requestHandler.addContact(name, phone);
                    break;
                case 3:
                    System.out.print("请输入要删除的联系人姓名：");
                    String delName = scanner.nextLine();
                    requestHandler.deleteContact(delName);
                    break;
                case 4:
                    System.out.print("请输入要更新的联系人姓名：");
                    String updateName = scanner.nextLine();
                    System.out.print("请输入新的电话号码：");
                    String newPhone = scanner.nextLine();
                    requestHandler.updateContact(updateName, newPhone);
                    break;
                case 5:
                    System.out.println("退出系统...");
                    System.out.println("客户端退出日志: 客户端已成功退出."); // 添加退出日志
                    return;
                default:
                    System.out.println("无效选项，请重新选择！");
            }
        }
    }
}
